<?php

namespace App\Http\Controllers\Store;

use App\Traits\Attendance\Attendance\Days;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    //

//	周插件
	public function week_plug(){
		$data=date("Y-m-d",time());
		$days=new Days($data,$data);
		$timestr = time();
		$now_day = $this->this_monday();
		//获取一周的第一天，注意第一天应该是星期天
//		$sunday_str = $timestr - $now_day*60*60*24;
		$str=[];
		for($i=0;$i<9;$i++){
			$str[]=date("Y-m-d",$now_day+60*60*24*7*$i);
		}


//		计算这周的开始和结束
		foreach($str as $kk=>$vv){
			$newArr[$kk][$vv]=$this->allData_week($vv);
		}

		foreach($newArr as $k=>$v){
			foreach($v as $key=>$value){
				foreach($value as $m=>$n){
					$newArr[$k][$key][$m]['day']=preg_replace('/^0+/','',date("d",strtotime($n['date'])));
					$newArr[$k][$key][$m]['week']=$days->getWeek($n['date']);
				}
			}
		}
		return $newArr;
	}



	public function this_monday($timestamp=0,$is_return_timestamp=true){
		static $cache ;
		$id = $timestamp.$is_return_timestamp;
		if(!isset($cache[$id])){
			if(!$timestamp) $timestamp = time();
			$monday_date = date('Y-m-d', $timestamp-86400*date('w',$timestamp)+(date('w',$timestamp)>0?86400:-/*6*86400*/518400));
			if($is_return_timestamp){
				$cache[$id] = strtotime($monday_date);
			}else{
				$cache[$id] = $monday_date;
			}
		}
		return $cache[$id];

	}

//	获取当前周的所有日期
	public function allData_week($strat_time){
		$time=strtotime($strat_time);
		for($i=0;$i<7;$i++){
			$data[]['date']=date("Y-m-d",$time+3600*24*$i);
//			$data[]['time']=date("Y-m-d",$time+3600*24*$i);

		}
		return $data;
	}

//  月插件
	public function month_plug(){
		for($i=1;$i<13;$i++){
			if($i<10){
				$data[]=date("Y").'-0'.$i;
			}else{
				$data[]=date("Y").'-'.$i;
			}
		}
		return $data;
	}

//	月插件二
	public function mon_plug(){
		$data=date("Y-m",time());
		$days=new Days($data,$data);
		for($i=0;$i<4;$i++){
			$month[]=date("Y-m", strtotime("$i month"));
		}
		foreach ($month as $kk=>$vv){
			$newArr[$kk]['month']=$vv;
			$newArr[$kk]['date']=$days->month_($vv)->collection;
		}
		foreach($newArr as $key=>$value){
			$Arr[$key]['month']=$value['month'];
			foreach($value['date'] as $k=>$v){
				$Arr[$key]['date'][$k]['time']=$v;
				$Arr[$key]['date'][$k]['week']=$days->getWeek($v);
				$Arr[$key]['date'][$k]['day']=preg_replace('/^0+/','',date("d",strtotime($v)));
			}
		}

		return $Arr;
	}

//	日期插件
	public function date_plug(){
		$year=date('Y');
		$days=new Days($year,$year);
		$newArr=[];
		for($i=0;$i<4;$i++){
			$newArr[]=$year+$i;
		}
		$str=[];
		foreach($newArr as $key=>$value){
			for($m=1;$m<13;$m++){
				$str[$value][]=$m;
			}
		}
		$array=[];
		foreach($str as $kk=>$vv){
			foreach($vv as $k=>$v){
				$array[$kk][$v][]=$days->month_($kk.'-'.$v);
			}
		}
		foreach($array as $key=>$vv){
			foreach($vv as $k=>$v){

			}
		}
		return $array;
	}
	public function month_all_day($month){
		$j = date("t",strtotime($month)); //获取当前月份天数
		$start_time = strtotime(date('Y-m-01'));  //获取本月第一天时间戳
		$array = array();
		for($i=0;$i<$j;$i++){
			$array[] = date('Y-m-d',$start_time+$i*86400); //每隔一天赋值给数组
		}
		return $array;
	}


	function getNextMonthDays($date){
		$timestamp=strtotime($date);
		$arr=getdate($timestamp);
		if($arr['mon'] == 12){
			$year=$arr['year'] +1;
			$month=$arr['mon'] -11;
			$firstday=$year.'-0'.$month.'-01';
			$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
		}else{
			$firstday=date('Y-m-01',strtotime(date('Y',$timestamp).'-'.(date('m',$timestamp)+1).'-01'));
			$lastday=date('Y-m-d',strtotime("$firstday +1 month -1 day"));
		}
		return array($firstday,$lastday);
	}
}
